<template>
  <div>
   <div class="header">
   <el-card class="box-card">
    <div>曹康</div>
     <!-- 搜索和添加按钮 -->
     <el-form  :inline="true" >
      <div class="style">
         <el-form-item>
        <el-input
        class="input"
        type="text"
        placeholder="根据用户名搜索"
        v-model="input"
        size="small"
        @change="onChange"
        @input="onInput"
        >
        </el-input>
     </el-form-item>
     <el-form-item>
      <el-button size="mini" @click="clear">清空</el-button>
      <el-button size="mini" type="primary" @click="search">搜索</el-button>
    </el-form-item>
      </div>
     <el-form-item type="flex" justify="end">
       <el-button size="mini" class="add" type="success" icon="el-icon-edit"  @click="dialogVisible = true">新增用户</el-button>
     </el-form-item>
    </el-form>
     <!-- 工具栏 -->
    <div class="tool">
      <i class="el-icon-info" />共{{total}}记录
    </div >
    <!-- 表格 -->
     <el-table
    ref="singleTable"
    :data="userList"
    highlight-current-row
    style="width: 100%">
    <el-table-column
       property="id"
      label="序号"
      width="50">
    </el-table-column>
    <el-table-column
      property="email"
      label="邮箱"
      width="120">
    </el-table-column>
    <el-table-column
      property="phone"
      label="联系电话"
      width="120">
    </el-table-column>
    <el-table-column
      property="username"
      label="用户名">
    </el-table-column>
    <el-table-column
      property="permission_group_title"
      label="权限组名称">
    </el-table-column>
    <el-table-column
      property="role"
      label="角色">
    </el-table-column>
    <el-table-column
      property="edit"
      label="操作"
      align="center"
      >
      <template v-slot='{row}'>
          <el-button size="mini" plain  type="primary" icon="el-icon-edit" circle  @click="onEdit(row)"></el-button>
           <el-button size="mini" v-if="row.id!=2" plain type="danger" icon="el-icon-delete" circle @click="onClickDel(row)"></el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页 -->
 <el-row type="flex" justify="end" style="height: 50px" align="middle">
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="page.page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="page.pagesize"
      layout="prev,pager,next,sizes,jumper"
      :total="total"
      background
    >
  </el-pagination>
 </el-row>
    </el-card>
   </div>
   <!-- 添加对话框 -->
   <el-dialog
     :title="titleText"
     :visible.sync="dialogVisible"
     width="50%"
     @close="onClose"
   >
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" style="width: 400px; margin-left:120px;">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item  label="邮箱" prop="email">
        <el-input v-model="form.email"></el-input>
      </el-form-item>
      <el-form-item v-if="!form.id" label="密码" prop="password">
        <el-input v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item label="角色" >
        <el-input v-model="form.role"></el-input>
      </el-form-item>
      <el-form-item label="权限组名称">
        <el-select v-model="form.permission_group_id" placeholder="请选择">
        <el-option v-for="item in userList" :key="item.id" :label='item.permission_group_title' :value="item.permission_group_id"></el-option>
      </el-select>
     </el-form-item>
     <el-form-item label="联系电话" prop="phone">
        <el-input v-model="form.phone"></el-input>
      </el-form-item>
      <el-form-item label="介绍">
        <el-input type="textarea" v-model="form.introduction"></el-input>
      </el-form-item>
     </el-form>
     <span slot="footer" class="dialog-footer">
       <el-button @click="dialogVisible = false">取 消</el-button>
       <el-button type="primary" @click="onSave">确 定</el-button>
     </span>
</el-dialog>
  </div>
</template>

<script>
import { list, add, remove, update } from '@/api/base/users.js'
const phonejson = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
const emailJson = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
export default {
  components: {
  },
  data () {
    return {
      input: '',
      page: {
        // total: 0,
        page: 1,
        keyword: '',
        pagesize: 10,
        disabled: 0
      },
      userList: [],
      total: 0,
      dialogVisible: false,
      form: {
        avatar: '',
        username: '',
        id: 0,
        // sex: '1',
        email: '',
        password: '',
        role: '',
        // permission_group_title: '',
        permission_group_id: 2,
        phone: '',
        introduction: ''
      },
      account: '',
      rules: {
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2到 10个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '邮箱格式不正确', trigger: 'blur', pattern: emailJson }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10个字符', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入正确的手机号', trigger: 'blur', phonejson, pattern: phonejson }
        ]
      }
    }
  },
  computed: {
    titleText () {
      return (this.form.id ? '编辑' : '添加') + '用户'
    }
  },
  created () {
    this.loadList()
  },
  methods: {
    async  loadList () {
      const res = await list(this.page)
      this.total = res.data.counts
      this.userList = res.data.list
      console.log(this.userList)
      console.log(this.userList[0].username)
    },
    handleCurrentChange (newPage) {
      this.page.page = newPage
      this.loadList()
    },
    // 点击搜索
    search () {
      const newArr = this.userList.filter(item => { // 一个数组 遍历每个对象
        return item.username.includes(this.input)
      })
      this.userList = newArr
      if (newArr.length === 0 || this.input === '') {
        this.$message.error('没有该用户名,请重新输入')
        this.loadList()
      }
    },
    onChange () {
      this.search()
    },
    // 值改变触发
    onInput (val) {
      if (this.input === '') {
        this.loadList()
      }
    },
    // 点击清空
    clear () {
      this.input = ''
      this.loadList()
    },
    // 当输入为空时 渲染列表
    updateList () {
      if (this.input === '') {
        this.loadList()
      }
    },
    // 点击编辑按钮
    onEdit (row) {
      this.dialogVisible = true
      // const res = await detail(row)
      // console.log(res)
      this.form.id = ''
      const obj = {}
      Object.keys(this.form).forEach(item => {
        Object.keys(row).forEach(i => {
          if (item === i) {
            obj[i] = row[i]
          }
        })
      })
      console.log(obj)
      this.form = obj
    },
    // 点击了确定
    async onSave () {
      try {
        await this.$refs.form.validate()
        if (this.form.id) {
          // 编辑
          await update(this.form)
          console.log(123)
          this.$message.success('编辑成功')
        } else {
          // 添加
          console.log(this.form)
          await add(this.form)
          this.$message.success('添加成功')
        }
        this.loadList()
        this.onClose()
      } catch (err) {
        console.log(err)
      }
      this.dialogVisible = false
    },
    // 点击了删除
    onClickDel (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 点击了确定
        await remove(row)
        this.$message.success('删除成功')
        this.loadList()
      }).catch(() => {})
    },
    // 监听对话框关闭
    onClose () {
      setTimeout(() => {
        this.form = {
          avatar: '',
          username: '',
          // sex: '1',
          email: '',
          password: '',
          role: '',
          // permission_group_title: '',
          permission_group_id: 2,
          phone: '',
          introduction: ''
        }
        this.$refs.form.resetFields()
      }, 200)
    }

  }
}
</script>

<style scoped lang='scss'>
::v-deep .el-card {
  .el-form{
    display: flex;
    .style{
      flex: 60%;
    }
  }
  margin: 20px 20px;
  .el-card__body{
    padding: 16px;
  }
  .add{
    // margin-left: 260px;
    align-content: flex-end;
    i{
     color: #fff;
    }

  }
    .tool {
      background-color: #f4f4f5;
      margin: 0;
      height: 30px;
      line-height: 30px;
      font-size: 13px;
       i {
         margin-left: 10px;
      margin-right: 5px;
       font-size: 16px;
      color: #909399;
    }
    }
}
::v-deep .el-dialog__body{
   padding: 30px 20px;
}
</style>
